C----------------------------------------------------------------------
C ADDS, SUBTRACTS, MULTIPLIES OR DIVIDES NUMBERS IN TWO ARRAYS.
C USEFUL FOR CHECKING OUT MODEL INPUT ARRAYS FOR BAD DATA VALUES.
C ZERO DIVIDES ARE GIVEN THE NO DATA VALUE.
C----------------------------------------------------------------------
      DIMENSION ARRAY1(200,200),ARRAY2(200,200),RESULTS(200,200)
      DIMENSION TEXT(4)
      CHARACTER *128 FILENAME,FORMT
      CHARACTER *1 OPERATION
      CHARACTER *2 VALUE

C OPEN AND READ ARRAY1 FILE
      write (6,'(a)') ' ENTER NAME OF FIRST FILE --> '
      read (5,'(A)') FILENAME
c      write (6,'(a)') ' ENTER NUMBER OF ROWS --> '
c      read (5,*)
       NROW = 31
c      write (6,'(a)') ' ENTER NUMBER OF COLUMNS --> '
c      read (5,*)
       NCOL = 157
c      write (6,'(a)') ' ENTER NO DATA INDICATOR VALUE --> '
c      read (5,*) 
      HNOFLO  = 9999
    3 write (6,'(a)') ' ENTER FORMAT OF FIRST FILE (EX 20F4.0)'
      read (5,'(A)') FORMT
      CALL FIXUP(FORMT)
      OPEN (UNIT=10,FILE=FILENAME,STATUS='OLD')
      REWIND (UNIT=10)
        DO 10 I=1,NROW
          READ (10,FORMT) (ARRAY1(i,j),J=1,NCOL)
   10   CONTINUE
      CLOSE (UNIT=10)

C OPEN AND READ ARRAY2 FILE
      write (6,'(a)') ' ENTER NAME OF SECOND FILE --> '
      read (5,'(A)') FILENAME
      write (6,'(a)') ' ENTER FORMAT OF SECOND FILE (EX 20F4.0)'
      read (5,'(A)') FORMT
      CALL FIXUP(FORMT)
      OPEN (UNIT=10,FILE=FILENAME,STATUS='OLD')
      REWIND (UNIT=10)
        DO 20 I=1,NROW
          READ (10,FORMT) (ARRAY2(i,j),J=1,NCOL)
   20   CONTINUE
      CLOSE (UNIT=10)
C OPEN FORMATTED OUTPUT FILE
      write (6,'(a)') ' ENTER NAME OF OUTPUT FILE --> '
      read (5,'(A)') FILENAME
      write (6,'(a)') ' ENTER FORMAT OF OUTPUT FILE (EX 20F4.0)'
      read (5,'(A)') FORMT
        CALL FIXUP(FORMT)
      OPEN (UNIT=16,FILE=FILENAME,STATUS='UNKNOWN')
      REWIND (UNIT=6)
C FIND OUT WHAT THEY WANT TO DO
   30 write (6,'(a)') ' ENTER THE OPERATION (EX *,/,+,-) --> '
      read (5,'(A)') OPERATION
      IF (OPERATION .EQ. '*') THEN
C DO THE MULTIPLICATION
        DO 50 I=1,NROW
          DO 40 J=1,NCOL
            IF (ARRAY1(i,j).EQ.HNOFLO .OR. ARRAY2(i,j).EQ.HNOFLO) THEN
              RESULTS(i,j)=HNOFLO
            ELSE
              RESULTS(i,j)=ARRAY1(i,j)*ARRAY2(i,j)
            END IF
   40     CONTINUE
   50   CONTINUE
      ELSEIF (OPERATION .EQ. '/') THEN
C DO THE DIVISION
        DO 70 I=1,NROW
          DO 60 J=1,NCOL
            IF (ARRAY1(i,j).EQ.HNOFLO .OR. ARRAY2(i,j).EQ.HNOFLO .OR.
     +          ARRAY2(i,j).EQ.0.0) THEN
              RESULTS(i,j)=HNOFLO
            ELSE
              RESULTS(i,j)=ARRAY1(i,j)/ARRAY2(i,j)
            END IF
   60     CONTINUE
   70   CONTINUE
      ELSEIF (OPERATION .EQ. '+') THEN
C DO THE ADDITION
        DO 90 I=1,NROW
          DO 80 J=1,NCOL
            IF (ARRAY1(i,j).EQ.HNOFLO .OR. ARRAY2(i,j).EQ.HNOFLO) THEN
              RESULTS(i,j)=HNOFLO
            ELSE
              RESULTS(i,j)=ARRAY1(i,j)+ARRAY2(i,j)
            END IF
   80     CONTINUE
   90   CONTINUE
      ELSEIF (OPERATION .EQ. '-') THEN
C DO THE SUBTRACTION
        DO 110 I=1,NROW
          DO 100 J=1,NCOL
            IF (ARRAY1(i,j).EQ.HNOFLO .OR. ARRAY2(i,j).EQ.HNOFLO) THEN
                 RESULTS(i,j)=HNOFLO
              ELSE
                 RESULTS(i,j)=ARRAY1(i,j)-ARRAY2(i,j)
            END IF
  100     CONTINUE
  110   CONTINUE
      ELSE
        GOTO 30
      ENDIF
C WRITE RESULTS AND CLOSE PRINT FILE
        DO 130 I=1,NROW
          WRITE (16,FORMT) (RESULTS(i,j),J=1,NCOL)
  130   CONTINUE
      CLOSE (UNIT=16)
C EXIT GRACEFULLY
      RETURN
      END



      SUBROUTINE FIXUP (FORMT)
C TRIM LEADING SPACES AND PUT PARENTHESIS ON FORMAT
      CHARACTER*32 FORMT
   10 J=1
   20 IF (FORMT(1:1) .NE. ' ') THEN
        I= INDEX(FORMT,' ')
        FORMT='('//FORMT(:I-1)//')'
        RETURN
      ELSE
        FORMT=FORMT(2:)
        J=I+1
      ENDIF
      IF (J .LT. 5) THEN
          GOTO 20
        ELSE
C SOMETHING WRONG- TOO MANY LEADING BLANK SPACES
          write (6,'(a)') ' ENTER FORMAT AGAIN (EX 20F4.0) -->'
          read (5,'(A)') FORMT
          GOTO 10
      ENDIF
      RETURN
      END

